﻿using System;
using System.Collections.Generic;
using System.Text;
using Windows.UI.Xaml.Data;
using Ch9_Portable.Models;

namespace Ch9RSS_Universal.Converters
{
    public class ThumbConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
                return value;

            List<Thumbnail> thumbs = (List<Thumbnail>)value;
            double desiredHeight = System.Convert.ToDouble(parameter);
            string targetThumb = "";
            foreach(Thumbnail t in thumbs)
            {
                if (System.Convert.ToDouble(t.height) >= desiredHeight)
                    return t.url;
                else
                    targetThumb = t.url;
            }

            return targetThumb;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class ThumbWidthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
                return value;

            List<Thumbnail> thumbs = (List<Thumbnail>)value;
            double desiredHeight = System.Convert.ToDouble(parameter);
            if (thumbs.Count > 0) {
                double ratio = System.Convert.ToDouble(thumbs[0].width) / System.Convert.ToDouble(thumbs[0].height);
                return (desiredHeight / ratio);
        }

            return desiredHeight;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
